Ext.namespace("org.microsite.group");

org.microsite.group.GroupEditor = Ext.extend(Ext.Window, {
    title : 'Edit/create group',
    closable : true,
    modal : true,
    width : 400,
    height : 200,
    layout : 'fit',
    
    data : null,
    parentObject : null,
    success : false,

    txtName : null,
    txtDescription : null,
    
    frm : null,

    initComponent : function() {
	    this.txtName = new Ext.form.TextField({
            name : 'name',
            fieldLabel : 'Group name',
            anchor : '100%',
            allowBlank : false,
            value : this.data ? this.data.name : '',
            emptyText : 'This field is required'
        });
	    this.txtDescription = new Ext.form.TextArea({
            name : 'description',
            fieldLabel : 'Description',
            value : this.data ? this.data.description : '',
            anchor : '100%'
        })
        this.frm = new Ext.FormPanel({
            bodyStyle : 'padding: 5px',
            items : [this.txtName, this.txtDescription]
        });

        this.btnOK = new Ext.Button({
            text : 'OK',
            handler : this.onOKClicked,
            scope : this
        });
        this.btnCancel = new Ext.Button({
            text : 'Cancel',
            handler : this.onCancelClicked,
            scope : this
        });
        

        Ext.apply(this, {
            items : [this.frm],
            buttons : [this.btnOK, this.btnCancel]
        });

        org.microsite.group.GroupEditor.superclass.initComponent.apply(this);

        this.addEvents({
            'change' : true
        });
    },
    
    onOKClicked : function(btn, e) {
        var group = Ext.apply(new Group(), this.frm.getForm().getValues(), this.data);
        if (this.parentObject)  group.parent = this.parentObject.id;
        this.success = true;
        this.fireEvent('change', group);
        this.close();
    },
    
    onCancelClicked : function(btn, e) {
        this.close();
    }
});